package chen.web.config;

public class EmailConfig extends Config{

	private static final String UseSMTPPropertyName = "useSMTP";
	private static final String SenderEmailPropertyName = "senderEmail";
	private static final String SMTPHostPropertyName = "smtpHost";
	private static final String UsernamePropertyName = "username";
	private static final String PasswordPropertyName = "password";
	
	private boolean useSMTP;
	private String senderEmail;
	private String smtpHost;
	private String username;
	private String password;
	
	private EmailConfig(Builder builder){
		this.useSMTP = builder.useSMTP;
		this.senderEmail = builder.senderEmail;
		this.smtpHost = builder.smtpHost;
		this.username = builder.username;
		this.password = builder.password;
	}
	
	protected EmailConfig(){}
	
	@Override
	protected void load(){
		this.useSMTP = Boolean.valueOf(this.getProperty(UseSMTPPropertyName));
		this.senderEmail = this.getProperty(SenderEmailPropertyName);
		if(this.senderEmail==null) this.senderEmail = "demo<demo@demo.com>";
		this.smtpHost = this.getProperty(SMTPHostPropertyName);
		this.username = this.getProperty(UsernamePropertyName);
		this.password = this.getProperty(PasswordPropertyName);
	}
	@Override
	public void persist(){
		this.setProperty(UseSMTPPropertyName, Boolean.toString(useSMTP));
		this.setProperty(SenderEmailPropertyName, senderEmail);
		this.setProperty(SMTPHostPropertyName, smtpHost);
		this.setProperty(UsernamePropertyName, username);
		this.setProperty(PasswordPropertyName, password);
	}
	
	public boolean isUseSMTP() {
		return useSMTP;
	}


	public String getSenderEmail() {
		return senderEmail;
	}

	public String getSmtpHost() {
		return smtpHost;
	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}
	
	public static class Builder{
		private boolean useSMTP;
		private String senderEmail;
		private String smtpHost;
		private String username;
		private String password;
		
		public EmailConfig build(){
			return new EmailConfig(this);
		}
		
		public Builder useSMTP(boolean useSMTP){
			this.useSMTP = useSMTP;
			return this;
		}
		public Builder senderEmail(String senderEmail){
			this.senderEmail = senderEmail;
			return this;
		}
		public Builder smtpHost(String smtpHost){
			this.smtpHost = smtpHost;
			return this;
		}
		public Builder username(String username){
			this.username = username;
			return this;
		}
		public Builder password(String password){
			this.password = password;
			return this;
		}
	}
}
